package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.DoctorScheduling;
import cn.sxt.clinic.pojo.ResultCode;
import cn.sxt.clinic.service.IDoctorSchedulingService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/doctorScheduling")
public class DoctorSchedulingController {

    @Autowired
    private IDoctorSchedulingService doctorSchedulingService;

    @RequiresPermissions("doctorScheduling:doctorSchedulingManager")
    @RequestMapping("/doctorSchedulingManager")
    public String doctorSchedulingManager(){
        return "doctorSchedulingManager";
    }

    @RequiresPermissions("doctorScheduling:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   String keyword,
                                   String last,
                                   String next){
        IPage<DoctorScheduling> page = new Page<>(pageNum,pageSize);
        QueryWrapper<DoctorScheduling> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(last)){
            wrapper.lt("start_date",last);
            wrapper.gt("end_date",last);
        }else if (StringUtils.isNotBlank(next)){
            wrapper.lt("start_date",next);
            wrapper.gt("end_date",next);
        }else{
            Date date =new Date();
            wrapper.lt("start_date",date);
            wrapper.gt("end_date",date);
        }
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("username",keyword);
        }
        IPage<DoctorScheduling> result =doctorSchedulingService.selectPage(page,wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return map;
    }

    @RequestMapping("/doctorSchedulingEdit")
    public String doctorScheduling(Model model,Long schedulingId){
        if (schedulingId!=null){
            DoctorScheduling doctorScheduling = doctorSchedulingService.getById(schedulingId);
            model.addAttribute("doctorScheduling",doctorScheduling);
        }
        return "doctorSchedulingEdit";
    }

    @RequiresPermissions("doctorScheduling:update")
    @RequestMapping("update")
    @ResponseBody
    public ResultCode update(DoctorScheduling doctorScheduling){
        UpdateWrapper<DoctorScheduling> wrapper=new UpdateWrapper<>();
        boolean update = doctorSchedulingService.update(doctorScheduling, wrapper);
        ResultCode resultCode=null;
        if (update){
            resultCode=new ResultCode(1,"修改成功");
        }else{
            resultCode=new ResultCode(0,"修改失败");
        }
        return resultCode;
    }

    @RequiresPermissions("doctorScheduling:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delele(Long schedulingId){
        boolean b = doctorSchedulingService.removeById(schedulingId);
        ResultCode resultCode=null;
        if (b){
            resultCode=new ResultCode(1,"删除成功");
        }else{
            resultCode=new ResultCode(0,"删除失败");
        }
        return resultCode;
    }

}

